package com.thuanhoang.boo;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import util.BooPreference;
import util.Constant;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.boo.service.SendDataService;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.thuanhoang.boo.adapter.CustomImageCoverAdapter;
import com.thuanhoang.boo.model.Product;

public class ProductDetailActivity  extends Activity{
	static Product product;
	private List<String> listImages;
	public int positionImageVp = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_detail_view);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText(product.getProductName());
		TextView productName = (TextView) findViewById(R.id.product_name);
		productName.setText(product.getProductName());
		TextView productDescription = (TextView) findViewById(R.id.product_description);
		productDescription.setText(product.getProductDesc());
		initAction();
		loadImageCoverToViewPager() ;
		onclickButton();
	}
	
	private void initAction() {
		View backButton = (View) findViewById(R.id.btn_back);
		backButton.setOnClickListener( new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		View btnAccount = (View)findViewById(R.id.btn_account);
		btnAccount.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(ProductDetailActivity.this, UpdateInfoActivity.class));
			}
		});
		// TODO Auto-generated method stub
		
	}
	
	

	public void onclickButton(){
		View likeregion = (View) findViewById(R.id.btnlike);
		likeregion.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				callLike();

				// TODO Auto-generated method stub

			}
		});
	}

	public void callLike(){
		try {
			SendDataService service = new SendDataService(this);
			AsyncHttpResponseHandler callBack = new AsyncHttpResponseHandler(){
				@Override
				public void onSuccess(String response) {
					successLike();
				}
				@Override
				public void onFailure(Throwable error) {
					Log.d("ThuanHM", "error is throw " + error.getMessage());
					errorRequest();
				}
			};
			JSONObject jsonParams = new JSONObject();
			jsonParams.put("productId", product.getProductId());
			String userId = new BooPreference(this).getPref()
					.getString(BooPreference.KEY_USER_ID, "").toString();
			jsonParams.put("custId", userId);


			service.sendData(Constant.API_LIKE_PRODUCT, jsonParams, callBack);
		}
		catch (Exception e){
			Log.d("ThuanHM", "error is " + e.getMessage());
			errorRequest();
		}
	}

	public void  successLike(){
		try {
			showAlert("Thông báo", "Cảm ơn bạn đã like sản phẩm của Boo!");
		}
		catch (Exception e)
		{
			Log.d("ThuanHM", "error is " + e.getMessage());
			errorRequest();
		}	
	}
	public void errorRequest(){
		showAlert("Error", "Đã có lỗi khi kết nối đến server. Xin vui lòng thử lại!");
	}
	public void showAlert(String title, String message){
		Builder build = new Builder(ProductDetailActivity.this).setCancelable(true)
				.setTitle(title)
				.setMessage(message)
				.setNegativeButton("Close", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		build.create().show();
	}


	private void loadImageCoverToViewPager() {
		// TODO Auto-generated method stub
		listImages = new ArrayList<String>();
		if(product.getProductImage()!=null){
			listImages.add(product.getProductImage());
		}
		if(product.getProductImage1()!=null){
			listImages.add(product.getProductImage1());
		}
		if(product.getProductImage2()!=null){
			listImages.add(product.getProductImage2());
		}
		if(product.getProductImage3()!=null){
			listImages.add(product.getProductImage());
		}



		int iNumberOfImage = listImages.size();

		// list image to display
		final List<ImageView> lstImage = new ArrayList<ImageView>();

		// get number dot image
		for (int i = 0; i < iNumberOfImage; i++) {
			String nameId = "imgCover" + (4 - i);
			int image1Id = getResources().getIdentifier(nameId,
					"id", getPackageName());
			ImageView imageView = (ImageView) findViewById(image1Id);
			imageView.setVisibility(View.VISIBLE);
			lstImage.add(imageView);
		}

		// view pager
		ViewPager mViewPager = (ViewPager) findViewById(R.id.view_pager);

		if (lstImage.size() > positionImageVp) {
			//
			lstImage.get(lstImage.size() - positionImageVp - 1)
			.setImageResource(R.drawable.ic_action_dot_select);
		}

		// on view page change page
		mViewPager
		.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {

				positionImageVp = position;

				for (int i = 0; i < lstImage.size(); i++) {
					lstImage.get(i).setImageResource(
							R.drawable.ic_action_dot);
				}

				//
				lstImage.get(lstImage.size() - position - 1)
				.setImageResource(
						R.drawable.ic_action_dot_select);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1,
					int arg2) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

		// create adapter and set adapter for this viewpager
		CustomImageCoverAdapter adpViewPager = new CustomImageCoverAdapter(this,
				this,listImages);
		adpViewPager.setZoom(false);
		mViewPager.setAdapter(adpViewPager);

		if (lstImage.size() >positionImageVp) {
			mViewPager.setCurrentItem(positionImageVp);
		}
		// get viewpager and set adapter for this viewpager
		//		adpViewPager = new ImageCoverAdapter(this, listImages);
		//		adpViewPager.setZoom(true);
		//		mViewPager.setAdapter(adpViewPager);
	}


	@Override
	protected void onResume() {
		super.onResume();
	}



}
